//
//  SecurityCenterViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
import SnapKit

class SecurityCenterViewController: BaseViewController {

    lazy var securityView:SecurityCenterView = {
       let view = SecurityCenterView(dataSource: securityModel)
        
       return view
    }()
    
    lazy var securityModel:SecurityCenterViewModel = {
        let viewModel = SecurityCenterViewModel.init(controller:self)
        
        return viewModel;
    }()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.title = kText("安全中心")
        view.themeBackground = ThemeManager.ThemeColorKeys.background
        view.addSubview(securityView)
        securityView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        self.archiveStates()
        NotificationCenter.default.addObserver(self, selector: #selector(refresh), name: NSNotification.Name.Exchange.securityDidChange, object: nil)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        AppSecurityInfo.default.readDic()
        securityView.tableView.reloadData()
    }
    
    func archiveStates() {
        CYMBShow.openIndicator(message: nil, view: self.view)
        AppSecurityInfo.default.archiveSecurityInfo(success: { (request) in
            CYMBShow.colseIndicator(view: self.view)
            self.securityView.setUpModel(model:AppSecurityInfo.default.model)
        }) { (response) in
            CYMBShow.colseIndicator(view: self.view)
        }
        
        
    }
    
    @objc func refresh() {
        self.archiveStates()
        
    }

    deinit {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.Exchange.securityDidChange, object: nil)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
